<?php

namespace Admin\Controller;

use Think\Controller;

class ArticleController extends CommonController
{
    public function lst()
    {
        $article=D('article');
        $res=$article->query("SELECT article.id, article.title, article.author, article.time, cate.name FROM article INNER JOIN cate ON  article.cate_id=cate.id");
        $this->assign('res',$res);
//        $cates=$article->query("SELECT cate.name FROM article INNER JOIN cate ON article.cate_id=cate.id");
//        $this->assign('cates',$cates);
//        $cate=$article->query("SELECT title FROM cate WHERE id={$res['cate_id']}");
//        $this->assign('cate',$cate);

//        $count = $article->count();// 查询满足要求的总记录数
//        $Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
//        $show  = $Page->show();// 分页显示输出
//        $list = $article->limit($Page->firstRow.','.$Page->listRows)->select();// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
//        $this->assign('list',$list);// 赋值数据集
//        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    public function add()//添加
    {
        $article=D('Article');
        if (IS_POST){
            $data['title']=I('post.title');
            $data['cate_id']=I('post.cate_id');
            $data['author']=I('post.author');
            $data['desc']=I('post.desc');
            $data['content']=I('post.content');
           
            $data['time']=time();

            if ($_FILES['pic']['tmp_name'] != '') {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = $_SERVER['DOCUMENT_ROOT']; // 设置附件上传根目录
                $upload->savePath = '/Public/Uploads/'; // 设置附件上传（子）目录
                // 上传单个文件
                $info = $upload->uploadOne($_FILES['pic']);
                if (!$info) {
                    $this->error($upload->getError());// 上传错误提示错误信息
                } else {
                    $data['pic'] = $info['savepath'] . $info['savename'];// 上传成功 获取上传文件信息
                }
            } else {

            }

            if ($article->create($data)){
                if ($article->add()){
                    $this->success('添加文章成功！',U('lst'));
                }else{
                    $this->error('添加文章失败！');
                }
            }else{
                $this->error($article->getError());
            }
            return;
        }
        $res=$article->query("SELECT * FROM cate");
        $this->assign('res',$res);
        $this->display();
    }
    public function edit()//修改
    {
        $article=D('article');
//        $res=$article->query("SELECT * FROM article");
        $res=$article->find(I('id'));
        $this->assign('res',$res);
        if (IS_POST){
            $data['title']=I('post.title');
            $data['author']=I('post.author');
            $data['content']=I('post.content');
            $data['id']=I('post.id');
            if ($article->create($data)){
                if ($article->save()){
                    $this->success('文章修改成功！',U('lst'));
                }else{
                    $this->error('文章修改失败！');
                }
            }else{
                $this->error($article->getError());
            }
            return;
        }
        $this->display();
    }
    public function del()//删除
    {
        $article=D('article');
        if ($article->delete(I('id'))){
            $this->success('删除成功！',U('lst'));
        }else{
            $this->error('删除失败！');
        }

    }

}

